﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProjectSemIII.Entity;
using ProjectSemIII.Process;

namespace ProjectSemIII.UserControl
{
    public partial class UserLoginControl : System.Web.UI.UserControl
    {
        private UserProcess userProcess;
        protected void Page_Load(object sender, EventArgs e)
        {
            userProcess = new UserProcess();
            if (Session["UserName"] == null || Session["UserName"].ToString() == "")
            {
                logined.Visible = false;
                notLogin.Visible = true;
            }
            else
            {
                logined.Visible = true;
                notLogin.Visible = false;
            }
        }

        protected void btSubmit_Click(object sender, EventArgs e)
        {
            error.Visible = false;
            if (checkValidateLogin())
            {
                string username = txtUserName.Text;
                string inputPass = txtPassword.Text;
                User u = userProcess.loginProcess(username, inputPass);
                if (u.Username == null)
                {
                    error.Visible = true;
                    error.InnerHtml = "<br /><br />Can't login to system!";
                }
                else
                {
                    Session["UserName"] = u.Username;
                    Session["Group"] = u.Role;
                    Response.Redirect(Request.RawUrl);
                }
            }
        }

        private bool checkValidateLogin()
        {
            bool flag = true;
            int result = userProcess.checkUserNamePassword(txtUserName.Text, txtPassword.Text);
            if (txtUserName.Text == "" || txtUserName.Text == null)
            {
                error.Visible = true;
                error.InnerHtml = "<br /><br />Please enter your username!";
                flag = false;
            }
            else
            {
                if (txtPassword.Text == "" || txtPassword.Text == null)
                {
                    error.Visible = true;
                    error.InnerHtml = "<br /><br />Please enter your password!";
                    flag = false;
                }
                else
                {
                    if (result != 10)
                    {
                        if (result == 100)
                        {
                            error.Visible = true;
                            error.InnerHtml = "<br /><br />Username does not exits!";
                            flag = false;
                        }
                        if (result == 90)
                        {
                            error.Visible = true;
                            error.InnerHtml = "<br /><br />Wrong Password!";
                            flag = false;
                        }
                        if (result == 80)
                        {
                            error.Visible = true;
                            error.InnerHtml = "<br /><br />Can't login to system!";
                            flag = false;
                        }
                    }
                }
            }
            return flag;
        }

        protected void btLogout_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            Response.Redirect(Request.RawUrl);
        }
    }
}